using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Quartz.Impl;
using Quartz.NET8.Web.Extensions;
using Quartz.NET8.Web.Filters;
using Quartz.NET8.Web.Utility;

namespace Quartz.NET8.Web;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddHttpContextAccessor();

        builder.Services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }).AddCookie(options =>
        {
            options.Cookie.HttpOnly = true;
            options.LoginPath = new PathString("/Home/Index");
            //  options.LogoutPath = "";
            options.ClaimsIssuer = CookieAuthenticationDefaults.AuthenticationScheme;
            options.SlidingExpiration = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
        });

        builder.Services.AddHttpClient();

        // Add services to the container.
        builder.Services.AddControllersWithViews()
            .AddNewtonsoftJson(op =>
            {
                op.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
                op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            });

        builder.Services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                            .RequireAuthenticatedUser()
                            .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        });

        builder.Services.AddHttpClient();
        builder.Services.AddHttpContextAccessor();
        builder.Services.AddMvc(options =>
        {
            options.Filters.Add(typeof(TaskAuthorizeFilter));
        });
        builder.Services.AddSession().AddMemoryCache();
        builder.Services.AddSingleton<IPathProvider, PathProvider>();
        builder.Services.AddTransient<HttpResultfulJob>();
        builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
        builder.Services.AddSingleton<Spi.IJobFactory, IOCJobFactory>();
        builder.Services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Home/Error");
        }

        IWebHostEnvironment env=app.Environment;
        app.UseQuartz(env).UseStaticHttpContext();

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseRouting();

        //app.UseAuthorization();

        app.MapControllerRoute(
            name: "default",
            pattern: "{controller=TaskBackGround}/{action=Index}/{id?}");

        app.Run();
    }
}
